from flask import Flask, render_template, jsonify
from covcharts import CovCharts
from covdata import EpidemicSituation
from stucharts import StuCharts
from globalchart import GlobalChart
import json

main = Flask(__name__)

cov_charts = CovCharts()
check_in_charts = StuCharts()
global_Charts = GlobalChart()


# 主页
@main.route('/')
def home_page():
    return render_template("homePage.html")


# 中国疫情
@main.route('/chinaCov', methods=["Get", "POST"])
def china_cov():
    return render_template("chinaCov.html")


# 打卡
@main.route('/checkIn', methods=["Get", "POST"])
def check_in():
    return render_template("checkIn.html")


# 世界疫情
@main.route('/worldCov', methods=["Get", "POST"])
def world_cov():
    return render_template("worldCov.html")


# 更新
@main.route('/updateCovData', methods=["GET", "POST"])
def update_cov_data():
    # 更新全局对象
    global cov_charts
    cov_charts = CovCharts()
    return jsonify('cov data update success')


@main.route("/pieChart", methods=["GET", "POST"])
def get_pie_chart():
    c = cov_charts.pie_base()
    return c.dump_options_with_quotes()


# 中国疫情界面图表
@main.route("/mapChart", methods=["GET", "POST"])
def get_map_chart():
    c = cov_charts.map_base()
    return c.dump_options_with_quotes()


@main.route("/geo_current_confirm", methods=["GET", "POST"])
def get_current_confirm_map_chart():
    c = cov_charts.geo_current_confirm()
    return c.dump_options_with_quotes()


@main.route("/geo_current_suspected", methods=["GET", "POST"])
def get_current_suspected_map_chart():
    c = cov_charts.geo_suspected_confirm()
    return c.dump_options_with_quotes()


@main.route("/line_serious_chart", methods=["GET", "POST"])
def get_line_serious_chart():
    c = cov_charts.line_serious_base()
    return c.dump_options_with_quotes()


@main.route("/lineChart", methods=["GET", "POST"])
def get_line_chart():
    c = cov_charts.line_base()
    return c.dump_options_with_quotes()


@main.route("/lineChartCurrentConfirm", methods=["GET", "POST"])
def get_line_current_confirm_chart():
    c = cov_charts.line_current_confirm_base()
    return c.dump_options_with_quotes()


@main.route("/BarChartCurrentConfirm", methods=["GET", "POST"])
def get_bar_current_confirm_chart():
    c = cov_charts.bar_base_of_current_confirm()
    return c.dump_options_with_quotes()


@main.route("/CovNums", methods=["GET", "POST"])
def get_cov_nums():
    cov_info = EpidemicSituation()
    cov_digit = cov_info.cov_nums
    # 新增风险地区键值对，具体是风险地区所在省份
    cov_digit['dangerAreas'] = len(cov_info.danger_areas)
    return jsonify(cov_digit)


@main.route('/deathRates', methods=["GET", "POST"])
def get_cov_rate_for_death():
    c = cov_charts.liquid_base_for_death()
    return c.dump_options_with_quotes()


@main.route('/cureRates', methods=["GET", "POST"])
def get_cov_rate_for_cure():
    c = cov_charts.liquid_base_for_cure()
    return c.dump_options_with_quotes()


# 打卡界面图表
@main.route('/checkMap', methods=["GET", "POST"])
def get_check_in_map():
    c = check_in_charts.Map3D_base()
    return c.dump_options_with_quotes()


@main.route('/checkLineStaff', methods=["GET", "POST"])
def get_check_in_line_of_staff():
    c = check_in_charts.line_base_of_staff()
    return c.dump_options_with_quotes()


@main.route('/checkLineGrades', methods=["GET", "POST"])
def get_check_in_line_of_grades():
    c = check_in_charts.line_base_of_grades()
    return c.dump_options_with_quotes()


@main.route('/checkFunnel', methods=["GET", "POST"])
def get_check_in_funnel():
    c = check_in_charts.Funnel_base_of_teacher()
    return c.dump_options_with_quotes()


@main.route('/checkPie', methods=["GET", "POST"])
def get_check_in_pie():
    c = check_in_charts.Pie_base_of_late_sign_in()
    return c.dump_options_with_quotes()


@main.route('/stuSignData', methods=["GET", "POST"])
def get_stu_sign_data():
    with open('./data/stu_data/stu_data.json') as file:
        json_data = json.load(file)
    return jsonify(json_data)


# 世界疫情
@main.route('/updateWorldData', methods=["GET", "POST"])
def update_world_data():
    global global_Charts
    global_Charts = GlobalChart()
    return jsonify('world data update success')


@main.route('/worldMap', methods=["GET", "POST"])
def get_world_map():
    c = global_Charts.map_globe_base()
    return c.dump_options_with_quotes()


@main.route('/worldBar', methods=["GET", "POST"])
def get_world_bar():
    c = global_Charts.bar_base()
    return c.dump_options_with_quotes()


@main.route('/worldCloud', methods=["GET", "POST"])
def get_world_cloud():
    c = global_Charts.word_cloud_base()
    return c.dump_options_with_quotes()


@main.route('/worldData', methods=["GET", "POST"])
def get_world_data():
    with open('./data/world_num.json') as file:
        json_data = json.load(file)
    return jsonify(json_data)


if __name__ == '__main__':
    main.run(host="127.0.0.1", port=5000, debug=True)
